package com.link.base.base.member;

import com.link.core.cllog.LCLogger;
import com.link.core.util.RedisUtil;
import com.link.core.util.redisclient.LinkRedisClient;


/**
 * 该工具类待整合
 * Redis Api
 *
 * @Author: liushoulong
 * @Date: 2018/11/30 14:39
 */
public class RedisApiUtils {

    /**
     * 设置key的有效期，单位是秒
     *
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, int exTime) {
        LinkRedisClient jedis = null;
        Long result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
            RedisUtil.returnResource(jedis);
        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    /**
     * exTime的单位是秒
     *
     * @param key
     * @param value
     * @param exTime
     * @return
     */
    public static String setEx(String key, String value, int exTime) {
        LinkRedisClient jedis = null;
        String result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
            RedisUtil.returnResource(jedis);
        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    public static String set(String key, String value) {
        LinkRedisClient jedis = null;
        String result = null;

        try {
            jedis = RedisUtil.getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
            RedisUtil.returnResource(jedis);
        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    /***
     * 获取数据
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        LinkRedisClient jedis = null;
        String result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.get(key);
        } catch (Exception e) {
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
            RedisUtil.returnResource(jedis);
        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    /**
     * 删除
     *
     * @param key
     * @return
     */
    public static Long del(String key) {
        LinkRedisClient jedis = null;
        Long result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            RedisUtil.returnResource(jedis);
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");

        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    /**
     * 设置指定key
     *
     * @param key
     * @param value
     * @return
     */
    public static Long setnx(String key, String value) {
        LinkRedisClient jedis = null;
        Long result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.setnx(key, value);
        } catch (Exception e) {
            RedisUtil.returnResource(jedis);
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
        }
        RedisUtil.returnResource(jedis);
        return result;
    }

    /**
     * 设置新值得到就值
     *
     * @param key   key值
     * @param value value 值
     * @return result 返回就值
     */
    public static String getSet(String key, String value) {
        LinkRedisClient jedis = null;
        String result = null;
        try {
            jedis = RedisUtil.getJedis();
            result = jedis.getSet(key, value);
        } catch (Exception e) {
            RedisUtil.returnResource(jedis);
            LCLogger.withException(e);
            LCLogger.withException("expire key:" + key + " error");
        }
        RedisUtil.returnResource(jedis);
        return result;
    }
}
